package cn.ekuma.epos.datalogic.define.dao.erp.subject;

import com.openbravo.bean.ProductCategory;
import com.openbravo.bean.erp.subject.Subject;
import com.openbravo.bean.erp.subject.SubjectProductCategory;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.DataRead;
import com.openbravo.data.loader.DataWrite;
import com.openbravo.data.loader.Datas;
import com.openbravo.data.loader.I_Session;
import com.openbravo.data.loader.KeyBuilder;
import com.openbravo.data.loader.TableDefinition;
import com.openbravo.data.model.Field;
import com.openbravo.format.Formats;
import cn.ekuma.data.dao.ModifiedLogDAO;
import cn.ekuma.data.dao.bean.I_ModifiedLogBean;
import cn.ekuma.epos.datalogic.define.dao.ProductCategoryDAO;

public class SubjectProductCategoryDAO extends ModifiedLogDAO<SubjectProductCategory>{

	ProductCategoryDAO productCategoryDAO;
	public SubjectProductCategoryDAO(I_Session s,ProductCategoryDAO productCategoryDAO) {
		super(s);
		this.productCategoryDAO=productCategoryDAO;
	}

	public SubjectProductCategoryDAO(I_Session s) {
		super(s);
	}

	@Override
	public SubjectProductCategory readValues(DataRead dr,SubjectProductCategory obj) throws BasicException {
		if(obj==null)
			obj=new SubjectProductCategory();
		obj.setId(dr.getString(1));
		obj.setSubjectId(dr.getString(2));
		obj.setProductCategoryID(dr.getString(3));
		if(productCategoryDAO!=null)
		  obj.setProductCategory(productCategoryDAO.find(KeyBuilder.getKey(dr.getString(3))));
		obj.setLastModified(dr.getTimestamp(4));
		return obj;
	}

	@Override
	public TableDefinition getTable() {
		return new TableDefinition(s,
                "SUBJECT_PRODUCT_CATEGORIES",
                new Field[]{
                   new Field("ID",Datas.STRING, Formats.STRING),
                   new Field("SUBJECT",Datas.STRING, Formats.STRING,Subject.class),
                   new Field("PRODUCTCATEGORIE",Datas.STRING, Formats.STRING,ProductCategory.class),
                   new Field(I_ModifiedLogBean.LASTMODIFIED,Datas.TIMESTAMP, Formats.TIMESTAMP)
                 },
                new int[]{0}
                );
	}

	@Override
	public void writeInsertValues(DataWrite dp, SubjectProductCategory obj)
			throws BasicException {
		dp.setString(1, obj.getId());
		dp.setString(2, obj.getSubjectId());
		dp.setString(3,obj.getProductCategoryID());
		dp.setTimestamp(4, obj.getLastModified());
	}

	@Override
	public Class getSuportClass() {
		return SubjectProductCategory.class;
	}

}
